﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using DTO;

namespace BLL
{
    public class WardBLL
    {
        HospitalDataContext DB = new HospitalDataContext();
        public IQueryable SelectWardChargeNurse()
        {
            return DB.Staffs.Join(
                DB.Jobs,
                a => a.StaffID,
                b => b.StaffID,
                (a, b) => new
                {
                    Name = a.FirstName + " " + a.LastName,
                    b.PositionID,
                    b.FinishDate,
                    b.JobID
                }
                ).Where(s => s.FinishDate == null).Join(
                DB.Positions,
                c => c.PositionID,
                d => d.PositionID,
                (c, d) => new
                {
                    c.Name,
                    d.Position1,
                    c.JobID

                }
                ).Where(s => s.Position1 == "Charge Nurse").Join(
                    DB.StaffOfWards,
                    g => g.JobID,
                    h => h.JobID,
                    (g, h) => new
                    {
                        g.Name,
                        h.WardID

                    }
                ).Join(
                DB.Wards,
                e => e.WardID,
                f => f.WardID,
                (e, f) => new
                {
                    e.Name,
                    f.WardID,
                    f.WardName,
                    f.Location,
                    f.TelExtn
                }
                ).Distinct().AsQueryable();

        }
        public IQueryable SelectAllWard()
        {
            return DB.Wards.AsQueryable();
        }
        public List<WardDTO> Select_Hoan()
        {
            List<WardDTO> listdto = new List<WardDTO>();
            var list = DB.Wards.AsQueryable();
            foreach(var l in list){
                WardDTO info  =new WardDTO();
                info.WardID=l.WardID;
                info.WardName = l.WardName;
                listdto.Add(info);

            }
            return listdto;
        }
        public List<WardInfoDTO> Get_Info_Ward(string wardid)
        {
            List<WardInfoDTO> listdto = new List<WardInfoDTO>();
            var list = (from ward in DB.Wards
                       join ChargeN in DB.ChargeNurses
                       on ward.WardID equals ChargeN.WardID into wcs
                       from wc in wcs.DefaultIfEmpty()
                       join staff in DB.Staffs
                       on wc.StaffID equals staff.StaffID into w_c_ss
                       from w_c_s in w_c_ss.DefaultIfEmpty()
                       where ward.WardID == wardid
                       select new
                       {
                           ward.WardID,
                           ward.WardName,
                           ward.Location,
                           ward.TelExtn,
                           wc.StaffID,
                           Name = w_c_s.FirstName + " " + w_c_s.LastName,
                       }
                    ).Distinct();
            foreach(var l in list){
                WardInfoDTO info  =new WardInfoDTO();
                info.WardID=l.WardID;
                info.WardName = l.WardName;
                info.Location = l.Location;
                info.TelExtn = l.TelExtn;
                info.StaffID=l.StaffID;
                info.Name=l.Name;
                listdto.Add(info);

            }
            Console.WriteLine(listdto.Count);
            return listdto;
        }
    }
}
